package cn.jekun.shop.app.action;

import javax.annotation.Resource;

import net.sf.json.JSONObject;
import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.component.core.Constants;
import cn.jekun.shop.common.service.MessageService;

/**
 * 消息中心
 * @author yuan
 *
 */
public class MessageAction extends AppBaseAction {
	
	@Resource(name = MessageService.BEAN_NAME)
	MessageService messageService;
	/**
	 * 用户打开商家的消息中心，返回用户和商家的消息
	 * @return
	 */
	public String getMessagesByUser(){
		JSONObject jsonObject = null;
		try{
			String shopId = request.getParameter("shopId");
			this.validateSign(getLoginSign(),getUserId(),shopId);//验证签名:loginSign+userId+shopId
			this.validateLoginApp();//验证是否登录
			
			String pageSize = this.request.getParameter("pageSize");
			String currentPage = this.request.getParameter("currentPage");
			String userId = this.getUserId();
			int ps=10,p=1;
			try {
				ps = Integer.parseInt(pageSize);
			} catch (Exception e) {
			}
			try {
				p = Integer.parseInt(currentPage);
			} catch (Exception e) {
			}
			page.setPageSize(ps);
			page.setPageNumber(p);
			
			jsonObject = messageService.getMessages(userId,shopId,"1",page);
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	/**
	 * 商家打开消息中心，返回商家和用户的消息
	 * @return
	 */
	public String getMessagesByShop(){
		JSONObject jsonObject = null;
		try{
			String receiveUserId = request.getParameter("receiveUserId");//接收消息的用户代码
			this.validateSign(getLoginSign(),getUserId(),receiveUserId);//验证签名:loginSign+userId+receiveUserId
			this.validateLoginApp();//验证是否登录
			
			String pageSize = this.request.getParameter("pageSize");
			String currentPage = this.request.getParameter("currentPage");
			int ps=10,p=1;
			try {
				ps = Integer.parseInt(pageSize);
			} catch (Exception e) {
			}
			try {
				p = Integer.parseInt(currentPage);
			} catch (Exception e) {
			}
			page.setPageSize(ps);
			page.setPageNumber(p);
			
			String shopId = this.getSessionUser().getShopId();
			
			jsonObject = messageService.getMessages(receiveUserId,shopId,"2",page);
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	
	/**
	 * 用户发送消息给商家
	 * @return
	 */
	public String sendMessage(){
		JSONObject jsonObject = null;
		try{
			String shopId = request.getParameter("shopId");
			String content = request.getParameter("content");
			this.validateSign(getLoginSign(),getUserId(),shopId,content);//验证签名:loginSign+userId+shopId+content
			this.validateLoginApp();//验证是否登录
			
			String userId = this.getUserId();
			
			jsonObject = messageService.sendMessage(userId,shopId,content);
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	
	/**
	 * 商家消息给用户
	 * @return
	 */
	public String replyMessage(){
		JSONObject jsonObject = null;
		try{
			String receiveUserId = request.getParameter("receiveUserId");//接收消息的用户代码
			String content = request.getParameter("content");
			this.validateSign(getLoginSign(),getUserId(),receiveUserId,content);//验证签名:loginSign+userId+receiveUserId+content
			this.validateLoginApp();//验证是否登录
			
			String userId = this.getUserId();
			String shopId = this.getSessionUser().getShopId();
			
			jsonObject = messageService.replyMessage(userId,shopId,receiveUserId,content);
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	
	/**
	 * 商家打开消息管理页面，返回有消息的会员列表，未读消息数量，最后一条消息的时间内容，倒叙
	 * @return
	 */
	public String getShopMessagers(){
		JSONObject jsonObject = null;
		try{
			this.validateSign(getLoginSign(),getUserId());//验证签名:loginSign+userId
			this.validateLoginApp();//验证是否登录
			
			String pageSize = this.request.getParameter("pageSize");
			String currentPage = this.request.getParameter("currentPage");
			int ps=10,p=1;
			try {
				ps = Integer.parseInt(pageSize);
			} catch (Exception e) {
			}
			try {
				p = Integer.parseInt(currentPage);
			} catch (Exception e) {
			}
			page.setPageSize(ps);
			page.setPageNumber(p);
			
//			String shopId = "1";
			String shopId = this.getSessionUser().getShopId();
			jsonObject = messageService.getShopMessagers(shopId,page);
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
}
